﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class RoyalItemHistoriesDAL : IRoyalItemHistoriesDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public RoyalItemHistoriesDAL()
        {

        }
        public DataSet LoadRoyalItemHistories(RoyalItemHistoryInfo hisInfo)
        {
            string strSQL;
            int EmpSeq = hisInfo.Employee == null ? 0 : hisInfo.Employee.Sequence;
            int ReceiveYear = 0;
            if (hisInfo.ReceiveDate.HasValue)
                ReceiveYear = DateTime.Parse(hisInfo.ReceiveDate.ToString()).Year;

            DataSet ds = null;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@ActiveRecord", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(1, "@Sequence", hisInfo.Sequence);
                dbManager.AddParameters(2, "@EmpSeq", EmpSeq);
                dbManager.AddParameters(3, "@ReceiveYear", ReceiveYear);

                strSQL = "SELECT h.ROYAL_ITEM_HIS_SEQ, h.EMP_SEQ, h.ROYAL_ITEM_REQ_SEQ, h.ROYAL_ITEM_SEQ"
                        + ", h.RECEIVED_DATE, h.POS_REF, h.POS_NAME, h.CLASS_NAME, h.SALARY, h.JOB_SITE_NAME"
                        + ", h.GAZETTE_NO1, h.GAZETTE_NO2, h.GAZETTE_NO3, h.GAZETTE_NO4, h.GAZETTE_NO5"
                        + ", h.GAZETTE_DATE, h.GET_DATE, h.RETURN_DATE, h.RETURN_AMOUNT"
                        + ", h.RECORD_STATUS, h.CREATED_BY, h.UPDATED_BY"
                    + " FROM HRM_PS_ROYAL_ITEM_HISTORIES h"
                        + " LEFT JOIN HRM_PS_EMPLOYEES e ON h.EMP_SEQ = e.EMP_SEQ"// ชั่วคราว
                    + " WHERE h.RECORD_STATUS = @ActiveRecord"
                    + (hisInfo.Sequence > 0 ? " AND h.ROYAL_ITEM_HIS_SEQ = @Sequence" : "")
                    + (EmpSeq > 0 ? " AND h.EMP_SEQ = @EmpSeq" : "")
                    + (ReceiveYear > 0 ? " AND YEAR(h.RECEIVED_DATE) = @ReceiveYear" : "")
                    + " ORDER BY h.RECEIVED_DATE DESC, h.ROYAL_ITEM_HIS_SEQ";

                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);

                return ds;
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }
        public IList<RoyalItemHistoryInfo> ListRoyalItemHistories(RoyalItemHistoryInfo hisInfo)
        {
            string strSQL;
            int EmpSeq = hisInfo.Employee == null ? 0 : hisInfo.Employee.Sequence;
            int ReceiveYear = 0;
            if (hisInfo.ReceiveDate.HasValue)
                ReceiveYear = DateTime.Parse(hisInfo.ReceiveDate.ToString()).Year;

            List<RoyalItemHistoryInfo> infoList = new List<RoyalItemHistoryInfo>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Sequence", hisInfo.Sequence);
                dbManager.AddParameters(2, "@EmpSeq", EmpSeq);
                dbManager.AddParameters(3, "@ReceiveYear", ReceiveYear);

                strSQL = "SELECT h.ROYAL_ITEM_HIS_SEQ, h.EMP_SEQ, h.ROYAL_ITEM_REQ_SEQ, h.ROYAL_ITEM_SEQ"
                        + ", h.RECEIVED_DATE, h.POS_REF, h.POS_NAME, h.CLASS_NAME, h.SALARY, h.JOB_SITE_NAME"
                        + ", h.GAZETTE_NO1, h.GAZETTE_NO2, h.GAZETTE_NO3, h.GAZETTE_NO4, h.GAZETTE_NO5"
                        + ", h.GAZETTE_DATE, h.GET_DATE, h.RETURN_DATE, h.RETURN_AMOUNT"
                        + ", h.RECORD_STATUS, h.CREATED_BY, h.UPDATED_BY"
                    + " FROM HRM_PS_ROYAL_ITEM_HISTORIES h"
                    + " LEFT JOIN HRM_PS_EMPLOYEES e ON h.EMP_SEQ = e.EMP_SEQ"// ชั่วคราว
                    + " WHERE h.RECORD_STATUS not like @DeleteRecord"
                    + " AND e.POS_SEQ IS NOT NULL"// ชั่วคราว
                    + (hisInfo.Sequence > 0 ? " AND h.ROYAL_ITEM_HIS_SEQ = @Sequence" : "")
                    + (EmpSeq > 0 ? " AND h.EMP_SEQ = @EmpSeq" : "")
                    + (ReceiveYear > 0 ? " AND YEAR(h.RECEIVED_DATE) = @ReceiveYear" : "")
                    + " ORDER BY h.RECEIVED_DATE DESC, h.ROYAL_ITEM_HIS_SEQ";

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    EmployeeDAL empDAL = new EmployeeDAL();
                    EmployeeInfo empInfo = new EmployeeInfo();
                    if (!rdr.IsDBNull(1))
                    {
                       // empInfo.Sequence = rdr.GetInt32(1);
                        IList<EmployeeInfo> empList = empDAL.ListEmployees(new EmployeeInfo(rdr.GetInt32(1)));
                        if (empList.Count > 0)
                            empInfo = empList[0];
                    }

                    RoyalItemDAL roiDAL = new RoyalItemDAL();
                    RoyalItemInfo roiInfo = new RoyalItemInfo();
                    if (!rdr.IsDBNull(3))
                    {
                        IList<RoyalItemInfo> roiList = roiDAL.ListRoyalItems(new RoyalItemInfo(rdr.GetInt32(3)));
                        roiInfo = roiList.Count > 0 ? roiList[0] : null;
                    }

                    RoyalItemHistoryInfo Info = new RoyalItemHistoryInfo(rdr.GetInt32(0)
                            , empInfo, rdr.IsDBNull(2) ? null : (Nullable<int>)rdr.GetInt32(2)
                            , roiInfo
                            , rdr.IsDBNull(4) ? (DateTime?)null : rdr.GetDateTime(4)
                            , rdr.IsDBNull(5) ? "" : rdr.GetString(5)
                            , rdr.IsDBNull(6) ? "" : rdr.GetString(6)
                            , rdr.IsDBNull(7) ? "" : rdr.GetString(7)
                            , rdr.IsDBNull(8) ? (double?)null : rdr.GetDouble(8)
                            , rdr.IsDBNull(9) ? "" : rdr.GetString(9)
                            , rdr.IsDBNull(10) ? "" : rdr.GetString(10)
                            , rdr.IsDBNull(11) ? "" : rdr.GetString(11)
                            , rdr.IsDBNull(12) ? "" : rdr.GetString(12)
                            , rdr.IsDBNull(13) ? "" : rdr.GetString(13)
                            , rdr.IsDBNull(14) ? "" : rdr.GetString(14)
                            , rdr.IsDBNull(15) ? (DateTime?)null : rdr.GetDateTime(15)
                            , rdr.IsDBNull(16) ? (DateTime?)null : rdr.GetDateTime(16)
                            , rdr.IsDBNull(17) ? (DateTime?)null : rdr.GetDateTime(17)
                            , rdr.IsDBNull(18) ? (double?)null : rdr.GetDouble(18)
                            , rdr.IsDBNull(19) ? "" : rdr.GetString(19)
                            , rdr.IsDBNull(20) ? null : (Nullable<int>)rdr.GetInt32(20)
                            , rdr.IsDBNull(21) ? null : (Nullable<int>)rdr.GetInt32(21)
                            );
                    infoList.Add(Info);
                }

                return infoList;
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }

        public void Insert(RoyalItemHistoryInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(19);

                dbManager.AddParameters(0, "@EmpSeq", Info.Employee == null ? (int?)null : Info.Employee.Sequence);
                dbManager.AddParameters(1, "@RoyalItemReqSeq", Info.RoyalItemReqSeq);
                dbManager.AddParameters(2, "@RoyalItemSeq", Info.RoyalItem == null ? (int?)null : Info.RoyalItem.Sequence);
                dbManager.AddParameters(3, "@ReceiveDate", Info.ReceiveDate);
                dbManager.AddParameters(4, "@PosRef", Info.PosRef);
                dbManager.AddParameters(5, "@PosName", Info.PosName);
                dbManager.AddParameters(6, "@ClassName", Info.ClassName);
                dbManager.AddParameters(7, "@Salary", Info.Salary);
                dbManager.AddParameters(8, "@JobSiteName", Info.JobSiteName);
                dbManager.AddParameters(9, "@GazetteNo1", Info.GazetteNo1);
                dbManager.AddParameters(10, "@GazetteNo2", Info.GazetteNo2);
                dbManager.AddParameters(11, "@GazetteNo3", Info.GazetteNo3);
                dbManager.AddParameters(12, "@GazetteNo4", Info.GazetteNo4);
                dbManager.AddParameters(13, "@GazetteNo5", Info.GazetteNo5);
                dbManager.AddParameters(14, "@GazetteDate", Info.GazetteDate);
                dbManager.AddParameters(15, "@RoyalDate", Info.RoyalDate);
                dbManager.AddParameters(16, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(17, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(18, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO HRM_PS_ROYAL_ITEM_HISTORIES(EMP_SEQ"
                       + ", ROYAL_ITEM_REQ_SEQ, ROYAL_ITEM_SEQ, RECEIVED_DATE"
                       + ", POS_REF, POS_NAME, CLASS_NAME, SALARY, JOB_SITE_NAME"
                       + ", GAZETTE_NO1, GAZETTE_NO2, GAZETTE_NO3, GAZETTE_NO4"
                       + ", GAZETTE_NO5, GAZETTE_DATE, GET_DATE"
                       + ", RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                   + " VALUES(@EmpSeq, @RoyalItemReqSeq, @RoyalItemSeq, @ReceiveDate"
                       + ", @PosRef, @PosName, @ClassName, @Salary, @JobSiteName"
                       + ", @GazetteNo1, @GazetteNo2, @GazetteNo3, @GazetteNo4"
                       + ", @GazetteNo5, @GazetteDate, @RoyalDate"
                       + ", @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(RoyalItemHistoryInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(19);

                dbManager.AddParameters(0, "@EmpSeq", Info.Employee == null ? (int?)null : Info.Employee.Sequence);
                dbManager.AddParameters(1, "@RoyalItemReqSeq", Info.RoyalItemReqSeq);
                dbManager.AddParameters(2, "@RoyalItemSeq", Info.RoyalItem == null ? (int?)null : Info.RoyalItem.Sequence);
                dbManager.AddParameters(3, "@ReceiveDate", Info.ReceiveDate);
                dbManager.AddParameters(4, "@PosRef", Info.PosRef);
                dbManager.AddParameters(5, "@PosName", Info.PosName);
                dbManager.AddParameters(6, "@ClassName", Info.ClassName);
                dbManager.AddParameters(7, "@Salary", Info.Salary);
                dbManager.AddParameters(8, "@JobSiteName", Info.JobSiteName);
                dbManager.AddParameters(9, "@GazetteNo1", Info.GazetteNo1);
                dbManager.AddParameters(10, "@GazetteNo2", Info.GazetteNo2);
                dbManager.AddParameters(11, "@GazetteNo3", Info.GazetteNo3);
                dbManager.AddParameters(12, "@GazetteNo4", Info.GazetteNo4);
                dbManager.AddParameters(13, "@GazetteNo5", Info.GazetteNo5);
                dbManager.AddParameters(14, "@GazetteDate", Info.GazetteDate);
                dbManager.AddParameters(15, "@RoyalDate", Info.RoyalDate);
                dbManager.AddParameters(16, "@RoyalItemHisSeq", Info.Sequence);
                dbManager.AddParameters(17, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(18, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_PS_ROYAL_ITEM_HISTORIES"
                                + " SET ROYAL_ITEM_REQ_SEQ = @RoyalItemReqSeq"
                                + ", ROYAL_ITEM_SEQ = @RoyalItemSeq"
                                + ", RECEIVED_DATE = @ReceiveDate"
                                + ", POS_REF = @PosRef"
                                + ", POS_NAME = @PosName"
                                + ", CLASS_NAME = @ClassName"
                                + ", SALARY = @Salary"
                                + ", JOB_SITE_NAME = @JobSiteName"
                                + ", GAZETTE_NO1 = @GazetteNo1"
                                + ", GAZETTE_NO2 = @GazetteNo2"
                                + ", GAZETTE_NO3 = @GazetteNo3"
                                + ", GAZETTE_NO4 = @GazetteNo4"
                                + ", GAZETTE_NO5 = @GazetteNo5"
                                + ", GAZETTE_DATE = @GazetteDate"
                                + ", GET_DATE = @RoyalDate"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE ROYAL_ITEM_HIS_SEQ = @RoyalItemHisSeq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void UpdateHistory(RoyalItemHistoryInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(12);

                dbManager.AddParameters(0, "@RoyalItemSeq", Info.RoyalItem == null ? (int?)null : Info.RoyalItem.Sequence);
                dbManager.AddParameters(1, "@ReceiveDate", Info.ReceiveDate);
                dbManager.AddParameters(2, "@GazetteNo1", Info.GazetteNo1);
                dbManager.AddParameters(3, "@GazetteNo2", Info.GazetteNo2);
                dbManager.AddParameters(4, "@GazetteNo3", Info.GazetteNo3);
                dbManager.AddParameters(5, "@GazetteNo4", Info.GazetteNo4);
                dbManager.AddParameters(6, "@GazetteNo5", Info.GazetteNo5);
                dbManager.AddParameters(7, "@GazetteDate", Info.GazetteDate);
                dbManager.AddParameters(8, "@RoyalDate", Info.RoyalDate);
                dbManager.AddParameters(9, "@Sequence", Info.Sequence);
                dbManager.AddParameters(10, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(11, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_PS_ROYAL_ITEM_HISTORIES"
                                + " SET ROYAL_ITEM_SEQ = @RoyalItemSeq"
                                + ", RECEIVED_DATE = @ReceiveDate"
                                + ", GAZETTE_NO1 = @GazetteNo1"
                                + ", GAZETTE_NO2 = @GazetteNo2"
                                + ", GAZETTE_NO3 = @GazetteNo3"
                                + ", GAZETTE_NO4 = @GazetteNo4"
                                + ", GAZETTE_NO5 = @GazetteNo5"
                                + ", GAZETTE_DATE = @GazetteDate"
                                + ", GET_DATE = @RoyalDate"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE ROYAL_ITEM_HIS_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(3, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE HRM_PS_ROYAL_ITEM_HISTORIES"
                    + " SET RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE ROYAL_ITEM_HIS_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
